#!/bin/bash

set -e

export CROSSPATH=/opt/yi/toolchain-sunxi-musl/toolchain/bin
export PATH=${PATH}:${CROSSPATH}

export TARGET=arm-openwrt-linux
export CROSS=arm-openwrt-linux
export BUILD=x86_64-pc-linux-gnu

export CROSSPREFIX=${CROSS}-

export STRIP=${CROSSPREFIX}strip
export CXX=${CROSSPREFIX}g++
export CC=${CROSSPREFIX}gcc
export LD=${CROSSPREFIX}ld
export AS=${CROSSPREFIX}as
export AR=${CROSSPREFIX}ar

SCRIPT_DIR=$(cd `dirname $0` && pwd)
cd $SCRIPT_DIR

cd libhelix-aac || exit 1
make clean
make -j $(nproc) || exit 1
mkdir ../live/lib/
cp -f ./libhelix-aac.a ../live/lib/
mkdir ../live/include/libhelix-aac
cp -f ./aaccommon.h ../live/include/libhelix-aac
cp -f ./aacdec.h ../live/include/libhelix-aac
cp -f ./statname.h ../live/include/libhelix-aac
cd ..

cd live || exit 1

sed 's/-DNO_OPENSSL=1 -DRTP_PAYLOAD_MAX_SIZE=1352/-DNO_OPENSSL=1 -DNO_STD_LIB -DRTP_PAYLOAD_MAX_SIZE=1352/' -i ./BasicUsageEnvironment/Makefile || exit 1
sed 's/-DNO_OPENSSL=1 -DRTP_PAYLOAD_MAX_SIZE=1352/-DNO_OPENSSL=1 -DNO_STD_LIB -DRTP_PAYLOAD_MAX_SIZE=1352/' -i ./testProgs/Makefile || exit 1
make clean
make all || exit 1

mkdir -p ../_install/bin || exit 1

cp ./rRTSPServer ../_install/bin || exit 1

arm-openwrt-linux-strip ../_install/bin/* || exit 1

#cd hlsProxy || exit 1
#make clean
#make || exit 1

#cp ./live555HLSProxy ../../_install/bin || exit 1

#arm-openwrt-linux-strip ../../_install/bin/* || exit 1
